package com.our123.common.web;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;

/**
 * 旧系统页面的访问过滤器
 * @author Jonhey
 *
 */
@SuppressWarnings("serial")
public class OldPageFilter implements Filter {
	//原系统URL访问路径表达式
	private final String[] regex = new String[] { "^/go.asp", "^/openarticle.asp", "^/wj.asp", "^/userinfo.asp", "^/flymore.asp" };
	//原系统栏目编号对应栏目CO
	private final Map<String, String> channels = new HashMap<String, String>() {
		{
			put("17", "novel");
			put("18", "poetry");
			put("19", "prose");
			put("20", "speech");
			put("21", "prosepoem");
			put("22", "serial");
			put("23", "script");
			put("24", "diary");
			put("25", "letter");
			put("26", "ancientpoem");
			put("27", "lyric");
			put("28", "notes");
		}
	};

	public void destroy() {}

	public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest) req;
		HttpServletResponse response = (HttpServletResponse) res;
		String path = request.getRequestURI();
		int type = -1;
		for (int i = 0; i < regex.length; i++) {
			if (path.matches(regex[i])) {
				type = i;
			}
		}
		boolean isSend = false;
		switch (type) {
		case 0:
			String id = request.getParameter("id");
			if (StringUtils.isNotBlank(id)) {
				isSend = true;
				response.sendRedirect("/article/" + id + ".jsp");
			}
			break;
		case 1:
			String sid = request.getParameter("id");
			if (StringUtils.isNotBlank(sid)) {
				isSend = true;
				response.sendRedirect("/article/" + sid + ".jsp");
			}
			break;
		case 2:
			String userName = request.getParameter("userName");
			if (StringUtils.isNotBlank(userName)) {
				isSend = true;
				userName = new String(userName.getBytes("ISO-8859-1"));
				response.sendRedirect("/space.jsp?u=" + userName);
			}
			break;
		case 3:
			String spaceName = request.getParameter("userName");
			if (StringUtils.isNotBlank(spaceName)) {
				isSend = true;
				spaceName = new String(spaceName.getBytes("ISO-8859-1"));
				response.sendRedirect("/space.jsp?u=" + spaceName);
			}
			break;
		case 4:
			String ttt = request.getParameter("ttt");
			if (StringUtils.isNotBlank(ttt) && channels.get(ttt) != null) {
				isSend = true;
				response.sendRedirect("/" + channels.get(ttt) + "/index.jsp");
			}
			break;
		default:
			isSend = true;
			response.setStatus(HttpServletResponse.SC_NOT_FOUND);
			break;
		}

		if (!isSend) {
			response.sendRedirect("/index.jsp");
		}
	}

	public void init(FilterConfig config) throws ServletException {}

}
